package com.erp.erp_ui.RoleGroupMember;

import com.erp.erp_entitys.LogDB.LogDBEntity;
import com.erp.erp_entitys.Role.RoleEntity;
import com.erp.erp_entitys.Role.RoleExample;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntity;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntityExample;
import com.erp.erp_entitys.RoleGroupMember.RoleGroupMemberEntity;
import com.erp.erp_entitys.RoleGroupMember.RoleGroupMemberEntityExample;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Role.IRoleService;
import com.erp.erp_servers.RoleGroup.IRoleGroupService;
import com.erp.erp_servers.RoleGroupMember.IRoleGroupMemberService;
import com.erp.erp_servers.log.ILogDBService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.erp.utils.WebApiUtils;
import com.ys.openapi.organization.department.Role;
import com.ys.openapi.organization.entity.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RoleGroupMemberControl
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-07-04 14:59
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "rolegroupmember")
 public class RoleGroupMemberControl extends Base {
    @Autowired
    IRoleGroupMemberService roleGroupMemberService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IRoleGroupService roleGroupService;
    @Autowired
    ILogDBService logDBService;
    //云枢接口角色方法类
    Role role=new Role();
    /**
     * 新增角色组成员
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody RoleGroupMemberEntity record){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            int num = roleGroupMemberService.insertSelective(record);
           try {
               //同步
               RoleExample example=new RoleExample();
               RoleExample.Criteria criteria = example.createCriteria();
               criteria.andCodeEqualTo(record.getRoleCode());
               List<RoleEntity> roleEntities = roleService.selectByExample(example);
               String roleName=roleEntities.get(0).getName();

               RoleGroupEntityExample example2=new RoleGroupEntityExample();
               RoleGroupEntityExample.Criteria criteria1 = example2.createCriteria();
               criteria1.andCodeEqualTo(record.getGroupCode());
               List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(example2);
               String groupName=roleGroupEntities.get(0).getName();
               RoleVo vo=new RoleVo();
               vo.setRoleName(roleName);
               vo.setRoleGroupName(groupName);
               WebApiUtils wau=new WebApiUtils();
               role.modify_role(vo,wau.getTokenString());
               logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","insert(新增)","角色组与角色关联=>"+vo.toString()));
               //结束
           }catch (Exception e){
               num=num+10000;
           }
            return R.build(num);
        }catch (Exception e ){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "insert2",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> insert2(String groupCode,String[] usercodes){//@RequestBody RoleGroupMemberEntity record
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            RoleGroupMemberEntity record=new RoleGroupMemberEntity();
            record.setGroupCode(groupCode);
            int num=0;
            int cun=0;
            for (int i=0;i<usercodes.length;i++){
                record.setRoleCode(usercodes[i]);
                num += roleGroupMemberService.insertSelective(record);
                try {
                    //同步
                    RoleExample example=new RoleExample();
                    RoleExample.Criteria criteria = example.createCriteria();
                    criteria.andCodeEqualTo(record.getRoleCode());
                    List<RoleEntity> roleEntities = roleService.selectByExample(example);
                    String roleName=roleEntities.get(0).getName();

                    RoleGroupEntityExample example2=new RoleGroupEntityExample();
                    RoleGroupEntityExample.Criteria criteria1 = example2.createCriteria();
                    criteria1.andCodeEqualTo(record.getGroupCode());
                    List<RoleGroupEntity> roleGroupEntities = roleGroupService.selectByExample(example2);
                    String groupName=roleGroupEntities.get(0).getName();
                    RoleVo vo=new RoleVo();
                    vo.setRoleId(roleName);
                    vo.setRoleName(roleName);
                    vo.setRoleGroupName(groupName);
                    WebApiUtils wau=new WebApiUtils();
                    role.modify_role(vo,wau.getTokenString());
                    logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","insert(新增)","角色组与角色关联=>"+vo.toString()));
                    //结束
                }catch (Exception e){
                    cun=cun+1;
                }
            }
            if(cun>0){
                num=num+10000;
            }
            return R.build(num);
        }catch (Exception e ){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 删除角色组成员
     * @param
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> delete(String[] codes,String groupCode){
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            RoleGroupMemberEntityExample example11=new RoleGroupMemberEntityExample();
            RoleGroupMemberEntityExample.Criteria criteria11 = example11.createCriteria();
            criteria11.andGroupCodeEqualTo(groupCode);
            List<String> list=new ArrayList<String>();
            for(int i=0;i<codes.length;i++){
                list.add(codes[i]);
            }
            criteria11.andRoleCodeIn(list);
            int num = roleGroupMemberService.deleteByExample(example11);

           try {
               //同步
               RoleExample example=new RoleExample();
               RoleExample.Criteria criteria = example.createCriteria();
               for(int i=0;i<list.size();i++){
                   criteria.andCodeEqualTo(list.get(i));
                   List<RoleEntity> roleEntities = roleService.selectByExample(example);
                   String roleName=roleEntities.get(0).getName();

                   RoleVo vo=new RoleVo();
                   vo.setRoleId(roleName);
                   vo.setRoleName(roleName);
                   vo.setRoleGroupName("");
                   WebApiUtils wau=new WebApiUtils();
                   role.modify_role(vo,wau.getTokenString());
                   logDBService.insert(new LogDBEntity(user.getName()+"("+user.getCode()+")","insert(新增)","角色组与角色关联=>"+vo.toString()));

               }
               //结束
           }catch (Exception e){
             num=num+10000;
           }
            return R.build(num);
        }catch (Exception e ){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据条件获取角色组成员列表
     * @param record
     * @return
     */
    @RequestMapping(value = "selectByPage",method = RequestMethod.POST)
    @ResponseBody
     public R<List<RoleGroupMemberEntity>> selectByPage(@RequestBody RoleGroupMemberEntity record){
       try {
          List<RoleGroupMemberEntity> roleGroupMemberEntities = roleGroupMemberService.selectByExample(record);

           return R.build(roleGroupMemberEntities);
       }catch (Exception e ){
           e.printStackTrace();
           return R.buildError(ResultStatusEnum.ERROR);
       }
   }

    /**
     * 根据角色组code获取角色列表
     * @param groupCode
     * @return
     */
    @RequestMapping(value = "selectByGroupCodeByPage")
    @ResponseBody
 public R<List<RoleEntity>> selectByGroupCodeByPage(String groupCode){
     try {
         R<List<RoleEntity>> build1=null;
         List<RoleEntity> roleEntities=new ArrayList<RoleEntity>();
         if(!groupCode.equals("")){
             RoleGroupMemberEntity record=new RoleGroupMemberEntity();
             record.setGroupCode(groupCode);
             List<RoleGroupMemberEntity> roleGroupMemberEntities = roleGroupMemberService.selectByExample(record);
             R<List<RoleGroupMemberEntity>> build =R.build(roleGroupMemberEntities);

            List<String> listRoleCode=new ArrayList<String>();
            for(int i=0;i<roleGroupMemberEntities.size();i++){
                listRoleCode.add(roleGroupMemberEntities.get(i).getRoleCode());
            }
             RoleExample example=new RoleExample();
             RoleExample.Criteria criteria = example.createCriteria();
             criteria.andCodeIn(listRoleCode);
             roleEntities = roleService.selectByExample(example);
               build1 =R.build(roleEntities);
             build1.setCount(build.getCount());
             build1.setTotal(build.getTotal());
             build1.setCode(build.getCode());
             build1.setStatus(build.getStatus());
             return build1;
         }else{
           return   R.build(roleEntities);
         }

     }catch (Exception e ){
         e.printStackTrace();
         return R.buildError(ResultStatusEnum.ERROR);
     }

 }
    @RequestMapping(value = "selectByGroupCode2ByPage")
    @ResponseBody
    public R<List<RoleEntity>> selectByGroupCode2ByPage(){
        try {
            List<RoleEntity> roleEntities=new ArrayList<RoleEntity>();
/*
                RoleGroupMemberEntity record=new RoleGroupMemberEntity();

                List<RoleGroupMemberEntity> roleGroupMemberEntities = roleGroupMemberService.selectByExample(record);
                List<String> listRoleCode=new ArrayList<String>();
                for(int i=0;i<roleGroupMemberEntities.size();i++){
                    listRoleCode.add(roleGroupMemberEntities.get(i).getRoleCode());
                }*/
                RoleExample example=new RoleExample();
                RoleExample.Criteria criteria = example.createCriteria();
              //  criteria.andCodeIn(listRoleCode);
                roleEntities = roleService.selectByExample(example);

            return R.build(roleEntities);
        }catch (Exception e ){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}
